<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\DB;

/**
 * 供应商活动
 *
 * @icon fa fa-circle-o
 */
class Activity extends Backend
{
    
    /**
     * Activity模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Activity');
        $this->supplier_id = input('supplier_id');
        $this->view->assign("isHouseList", $this->model->getIsHouseList());
        $this->view->assign("categoryList", $this->model->getCategoryList());
    }
    
    /**
     * 查看
     */
    public function index()
    {
        $supplier = model('supplier')->field('type')->where('id',$this->supplier_id)->find();

        //设置过滤方法
        $this->relationSearch = true;
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with('supplier')
                    ->where($where)
                    ->where('is_delete','0')
                    ->where('supplier_id',$this->supplier_id)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with('supplier')
                    ->field('activity.*,supplier.name as supplier_name')
                    ->where($where)
                    ->where('is_delete','0')
                    ->where('supplier_id',$this->supplier_id)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = addtion($list, 'city_id');
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        if($supplier['type'] == 1)return $this->view->fetch('site');// 场地商
        if($supplier['type'] == 3)return $this->view->fetch('master');// 主讲老师

        return $this->view->fetch();// 活动商
    } 
    
    /**
     * 详情
     */
    public function detail($ids)
    {
        $supplier = $this->model->alias('a')
        ->join('qz_supplier s','a.supplier_id=s.id')
        ->field('s.type,a.is_site')->where('a.id',$ids)->find();
        if($supplier['type'] == 1)
        {
            $row = DB::name('activity')->field('name,address,max_num,fee,is_house,square,begintime,endtime,plan,extra_service,is_metro,park_number,is_ticket,desc')->where('id',$ids)->find();

           $extraServices = $this->model->getExtraServiceAttr($row['extra_service']);
           $row['extra_service'] = $extraServices;
           $row['is_metro'] = $row['is_metro']==1?"有":"无";
           $row['park_number'] =$this->model->getPackNumber($row['park_number']);
           $row['is_ticket'] =$row['is_ticket']==1?"是":"否";
           $row['is_house'] = $row['is_house']==1?"包含":"不包含";
           $row['begintime'] = $this->model->getBusinessTime($row['begintime']);
           $row['endtime'] = $this->model->getBusinessTime($row['endtime']);
        }
        else if($supplier['type'] == 2)
        {
            if($supplier['is_site'])
            {
                $row = DB::name('activity')->field('name,category,time,fee,max_num,include,city_id,is_site,site_name,site_address,plan,desc,notes,is_ticket')->where('id',$ids)->find();
            }
            else
            {
                $row = DB::name('activity')->field('name,category,time,fee,max_num,include,city_id,is_site,plan,desc,notes,is_ticket')->where('id',$ids)->find();
                $row['is_ticket'] =$row['is_ticket']==1?"是":"否";
                $row['is_site'] = $row['is_site']==1?"包含":"不包含";
                $row['category'] = $this->model->getCategoryTextAttr($row['category'],$row);
                $row['city_id'] = $this->model->getCity($row['city_id']);
            }
            
        }
        else
        {
            $row = DB::name('activity')->field('name,category,time,fee,plan,desc,notes')->where('id',$ids)->find();
            $row['category'] = $this->model->getCategoryText2Attr($row['category']);
        }

        if($row['plan'])
        {
            $row['plan'] = json_decode($row['plan'],true);
        }
        if($row['desc'])
        {
            $row['desc'] = json_decode($row['desc'],true);
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
